#include<iostream>
#include<algorithm>

using namespace std;

int sum[31][30000];
int max_money=0;
double Q;
int N;
double sum=0;
int count[31];
int count_size=0;

int main(void)
{
	int i,j,m;
	char ch;
	double temp;
	while( cin>>Q && cin>>N && N!=0)
	{
		memset(sum,0,sizeof(int)*31*30000);
		memset(count,0,sizeof(int)*31);
		count_size = 0;
		for(i=0;i<N;i++)
		{
			cin>>m;
			sum=0;
			for(j=0; j<m; j++)
			{
				cin >> ch;
				if(ch!="A" && ch!="B" && ch!="C") break;
				cin >> ch;
				cin >> temp;
				if(temp > 600.0) break;
				sum=sum+temp;
			}
			if(j==m && sum<=1000)
			{
				
			}
		}
	}

}